﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text.RegularExpressions;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using Newtonsoft.Json.Linq;

namespace ZLCMS.Web.Tools
{
    public partial class GenarateDoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string api = RedisCache.GetStringValue("SYS_CFG#MEMBER_SERVICE_IP", "") + "/api/Score/UserScoreForOrder";

            var cpScoreResult = ZlanApiCaller.Call<JObject>(api, new { MemberAccount = "hlf", Score = 100, OrderNo = "NOOOOOOOO1" });
            if (cpScoreResult["ErrorCode"].Value<int>() != 0)
            {
                CMSKits.WriteLog("", cpScoreResult["ErrorMessage"].Value<string>(), "");
            }
                        
        }
        XmlNodeList MemberNodeList { get; set; }
        //生成接口文档
        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/bin/zlcms.business.test.xml"));
            MemberNodeList = doc.SelectNodes(@"//member");

            using (ISession session = SessionFactoryHelper.CreateSession())
            {

                foreach (XmlNode member in MemberNodeList)
                {
                    string memberName = member.Attributes["name"].InnerText;

                    //
                    Regex paramReg = new Regex(@"T:[\w,.]*Parameter$");

                    if (paramReg.IsMatch(memberName))
                    {
                       XmlNode node = member.SelectSingleNode("./summary");
                        string key = memberName.Substring(memberName.LastIndexOf(".") + 1).Replace("Parameter", "");
                        string memo = member.SelectSingleNode("./summary").InnerText;
                        session.CreateSQLQuery("insert into app_command_doc_part(KIND,`KEY`,TYPE,MEMO) values('PARAM',:k,'T',:m)")
                            .SetParameter("k", key)
                            .SetParameter("m", memo)
                            .ExecuteUpdate();
                        Response.Write("参数：" + memberName + "<br/>");
                        HandleNode(member, memberName.Substring(2), session, "PARAM", key);
                    }

                    Regex resultReg = new Regex(@"T:[\w,.]*Result$");
                    if (resultReg.IsMatch(memberName))
                    {
                        string key = memberName.Substring(memberName.LastIndexOf(".") + 1).Replace("Result", "");
                        string memo = member.SelectSingleNode("./summary").InnerText;
                        session.CreateSQLQuery("insert into app_command_doc_part(KIND,`KEY`,TYPE,MEMO) values('RESULT',:k,'T',:m)")
                            .SetParameter("k", key)
                            .SetParameter("m", memo)
                            .ExecuteUpdate();
                        Response.Write("输出：" + memberName + "<br/>");
                        HandleNode(member, memberName.Substring(2), session, "RESULT", key);
                    }

                    Regex cmdReg = new Regex(@"T:[\w,.]*Command$");
                    if (cmdReg.IsMatch(memberName))
                    {
                        string key = memberName.Substring(memberName.LastIndexOf(".") + 1).Replace("Command", "");
                        string memo = member.SelectSingleNode("./summary").InnerText;
                        session.CreateSQLQuery("insert into app_command_doc_part(KIND,`KEY`,TYPE,MEMO) values('COMMAND',:k,'T',:m)")
                            .SetParameter("k", key)
                            .SetParameter("m", memo)
                            .ExecuteUpdate();
                        Response.Write("接口：" + memberName + "<br/>");
                    }
                }

            }
        }

        //输出参数相关的属性
        protected void HandleNode(XmlNode node, string clssName, ISession session, string kind, string key)
        {
            foreach (XmlNode field in MemberNodeList)
            {
                string fieldName = field.Attributes["name"].InnerText;
                
                string memo = field.SelectSingleNode("./summary").InnerText;
                session.CreateSQLQuery("insert into app_command_doc_part(KIND,`KEY`,TYPE,MEMO) values(:kind,:k,'P',:m)")
                    .SetParameter("kind", kind)
                    .SetParameter("k", key)
                    .SetParameter("m", memo)
                    .ExecuteUpdate();
                //
                Regex paramReg = new Regex("P:" + clssName + @".[\w,.]*");
                if (paramReg.IsMatch(fieldName))
                {
                    Response.Write("属性：" + fieldName + "<br/>");
                }
            }
        }
    }
}